import app from "@/main";

import store from "../store";
import { getShopDetailNoload } from "@/service/api/sp-store/index.js";
const _this = this;
const callPhone = (phone) => {
  if (!phone) return;
  if (typeof phone != String) phone = phone.toString();
  uni.makePhoneCall({
    phoneNumber: phone,
  });
};
const lookLocation = (latitude, longitude, name, address) => {
  if (!latitude || !longitude) return;
  uni.openLocation({
    latitude,
    longitude,
    name,
    address,
    success: function () {
      console.log("success");
    },
  });
};
const toFullUrl = (value) => {
  if (!value) {
    return;
  }
  let fileBaseUrl = process.env.VUE_APP_BASE_FileUrl;

  if (
    value.startsWith("http") ||
    value.startsWith("/static/") ||
    value.startsWith("/pages/") ||
    value.startsWith("data:image")
  ) {
    return value;
  } else {
    if (!value.startsWith("/") && !value.startsWith("..")) {
      value = "/" + value;
    }
    return fileBaseUrl + value;
  }
};
const toTab = ({ key }) => {
  const CurrentPage = getCurrentPages();
  console.log(CurrentPage);
  store.commit("sp-common/base/SET_ACTIVE", key);
  uni.redirectTo({
    url: "/pages/home/index",
  });
};
const jumpPage = (path, params) => {
  // path   可以为name也可以为path    如果是back 默认是返回上一级
  // redirect = uni.reLaunch
  if (!path) return;
  if (!params?.redirect) {
    if (path.startsWith("/pages")) {
      app.$Router.push({
        path,
        query: params,
      });
    } else if (path.startsWith("back")) {
      app.$Router.back(params || 0);
    } else {
      app.$Router.push({
        name: path,
        params,
      });
    }
  } else {
    delete params.redirect;
    app.$Router.replaceAll({
      name: path,
      params,
    });
    if (path.startsWith("/pages")) {
      app.$Router.replaceAll({
        path,
        query: params,
      });
    } else {
      app.$Router.replaceAll({
        name: path,
        params,
      });
    }
  }
};
const transitionDistance = (m) => {
  if (!m) return;
  let n = "";
  if (m) {
    n = m >= 1000 ? `${(m / 1000).toFixed(2)}km` : `${m.toFixed(2)}m`;
  } else {
    n = "0m";
  }
  return n;
};
// 保留两位小数,不四舍五入
const toFixedNum = (v) => {
  let toStr = v.toString();
  let find = toStr.indexOf(".");
  let rs = "";
  if (find > -1) {
    // 截取两位小数点 或 补0
    let sp = toStr.split(".");
    let len = sp[1].toString().split("").length;
    if (len > 1) {
      rs = toStr.substring(0, find + 3);
    } else {
      rs = toStr + "0";
    }
  } else {
    rs = Number(v).toFixed(2);
  }
  return rs;
};
const verifyShop = async (options) => {
  let shopInfo = await getShopDetailNoload({
    id: store.state["sp-store/index"].pubShopId,
  });
  let { allStatus } = shopInfo;

  // 门店总状态 0营业中 1休息中 2禁用中
  if (allStatus == 1) {
    uni.showModal({
      content:
        options?.type == "store"
          ? "门店已打烊，暂不支持充值"
          : "当前门店已打烊，请重新选择门店下单",
      showCancel: false,
      confirmText: "我知道了",
      success: function (res) {
        if (res.confirm) {
          app.$Router.back(1);
        }
      },
    });
    return false;
  } else if (allStatus == 2) {
    uni.showModal({
      content:
        options?.type == "store"
          ? "门店已被禁用，暂不支持充值"
          : "当前门店已关停，无法查看门店详情",
      showCancel: false,
      confirmText: "我知道了",
      success: function (res) {
        if (res.confirm) {
          app.$Router.back(1);
        }
      },
    });
    return false;
  } else if (allStatus == 0) {
    return true;
  }
};

export const myTool = {
  callPhone,
  lookLocation,
  toFullUrl,
  jumpPage,
  toTab,
  transitionDistance,
  toFixedNum,
  verifyShop,
};
